using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace _1b
{
      public partial class Form1 : Form
    {
        private Random random = new Random();
        private int Answer;
        private int score = 0;
        private int timeRemain;
        private int questionNum = 0;
        private int userTime = 15;
        private int userNum = 10;

        public Form1()
        {
            InitializeComponent();
            // 开始答题前禁用答题按钮控件
            textBoxAnswer.Enabled = false;
            buttonSubmit.Enabled = false;
        }

        // 开始答题，显示题目和剩余时间
        private void buttonStart_Click(object sender, EventArgs e)
        {
            int number;
            int time;

            if (int.TryParse(textBoxNum.Text, out number) && int.TryParse(textBoxTime.Text, out time))
            {
                userNum = number;
                userTime = time;
            }
            // 开启答题控件
            buttonSubmit.Enabled = true;
            textBoxAnswer.Enabled = true;
            buttonStart.Enabled = false; 
            GenerateQuestion();
            StartTimer();
        }

        // 利用Random随机生成题目
        private void GenerateQuestion()
        {
            int num1 = random.Next(1, 100);
            int num2 = random.Next(1, 100);
            int operation = random.Next(0, 2); // 0 = 加法, 1 = 减法

            switch (operation)
            {
                case 0: // 加法
                    Answer = num1 + num2;
                    labelQuestion.Text = $"{num1} + {num2} = ?";
                    break;
                case 1: // 减法
                    Answer = num1 - num2;
                    labelQuestion.Text = $"{num1} - {num2} = ?";
                    break;
            }

            // 更新当前题目进度
            labelProgress.Text = $"目前第 {questionNum + 1} 题，共 {userNum} 题";
        }

        // 启动计时器
        private void StartTimer()
        {
            timeRemain = userTime;
            timer1.Start();
        }

        // 提交答案按钮事件处理
        private void buttonSubmit_Click(object sender, EventArgs e)
        {
            int userAnswer;

            if (int.TryParse(textBoxAnswer.Text, out userAnswer))
            {
                CheckAnswer(userAnswer);
            }
            else
            {
                MessageBox.Show("请输入有效的数字！");
            }
        }

        // 检查答案
        private void CheckAnswer(int userAnswer)
        {
            switch (userAnswer == Answer)
            {
                case true:
                    score++;
                    labelFeedback.Text = "答案正确！";
                    textBoxAnswer.BackColor = System.Drawing.Color.LightGreen;
                    //变化答题卡颜色表示正确/错误
                    break;
                case false:
                    labelFeedback.Text = "答案错误！";
                    textBoxAnswer.BackColor = System.Drawing.Color.Pink;
                    break;
            }

            questionNum++;
            if (questionNum < userNum)
            {
                GenerateQuestion(); // 下一题
                textBoxAnswer.Clear();
                textBoxAnswer.BackColor = System.Drawing.Color.White;
                StartTimer();
            }
            else
            {
                timer1.Stop();
                MessageBox.Show($"答题结束！总分：{score}/{userNum}");
            }
        }

        // Timer 事件处理，超时跳过题目
        private void timer1_Tick(object sender, EventArgs e)
        {
            if (timeRemain > 0)
            {
                timeRemain--;
                labelTimer.Text = $" {timeRemain} 秒";
            }
            else
            {
                timer1.Stop();
                MessageBox.Show("超时！自动跳过此题！");
                CheckAnswer(int.MinValue); // 超时当作错误处理
            }
        }

        private void Form1_Load(object sender, EventArgs e)
        {

        }
    }
}